import {ACTIVE_TASK, ADD_TASK, COMPLETE_TASK, DEL_TASK} from "./actions";

function add(state = [], action) {
    if (action.type == ADD_TASK) {
        if (state.filter(item => item.text === action.task.text).length === 0) {
            return [...state, action.task];
        }
    }
}

function complete(state = [], action) {
    if (action.type == COMPLETE_TASK) {
        return state.map((item) => {
            if (item.text === action.task.text) {
            item.completed = !action.task.completed;
        }
        return item;
    })
    }
}

function active(state = [], action) {
    if (action.type == ACTIVE_TASK) {
        return state.map((item) => {
            if (item.text === action.task.text) {
            item.actived = !action.task.actived;
        }
        return item;
    })
    }
}

function del(state = [], action) {
    if (action.type == DEL_TASK) {
        return state.filter((item) => item.text !== action.task.text);
    }
}

export function reducer(...args) {
    return add(...args) || del(...args) || complete(...args) || active(...args) || args[0];
}
